package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.model.BankcardData;
import com.youi.model.BankcardObj;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.BankcardrecordService;
import com.youi.service.GroupleaderService;
import com.youi.service.GroupleaderbankService;
import com.youi.third.cloundmessage.BankcardUtil;
import com.youi.entity.Bankcardrecord;
import com.youi.entity.Groupleader;
import com.youi.entity.Groupleaderbank;
import com.youi.entity.User;
import com.alibaba.fastjson.JSON;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JedisUtils;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 团长提现信息控制层
* @author Administrator
*/
@Controller
public class GroupleaderbankController  extends HttpRequestManage{

		@Autowired
		private GroupleaderbankService groupleaderbankService;
		@Autowired
		private BankcardrecordService bankcardrecordService;
		@Autowired
		private GroupleaderService groupleaderService;

		/**
		 * M-28
		 * 添加或编辑团长提现资料
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/addOrEditGroupleaderbank")
		public String addOrEditGroupleaderbank(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Long id=optLong("id",request);
			Groupleaderbank groupleaderbank=null;
			if (id!=null) {
				groupleaderbank=groupleaderbankService.get(id);
				Integer dayNum=DateUtil.getDateDayLenth(DateUtil.simpdfyMdHms.format(groupleaderbank.getGmtModify()),DateUtil.simpdfyMdHms.format(new Date()));
				if (dayNum<30) {
					return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "距离上次修改时间不足一个月无法修改资料");
				}
			}else {
				groupleaderbank=new Groupleaderbank();
				groupleaderbank.setDelStatus(DelStatus.NODEL);
				groupleaderbank.setGmtCreated(new Date());
				groupleaderbank.setLeaderId(user.getLeaderId());
				groupleaderbank.setStatus(1);
			}
			String realName=optString("realName",request);
			if (realName!=null){
				groupleaderbank.setRealName(realName);
			}
			String idCard=optString("idCard",request);
			if (idCard!=null){
				groupleaderbank.setIdCard(idCard);
			}
			String idCardFront=optString("idCardFront",request);
			if (idCardFront!=null){
				groupleaderbank.setIdCardFront(idCardFront);
			}
			String idCareBehind=optString("idCareBehind",request);
			if (idCareBehind!=null){
				groupleaderbank.setIdCareBehind(idCareBehind);
			}
			String handInCard=optString("handInCard",request);
			if (handInCard!=null){
				groupleaderbank.setHandInCard(handInCard);
			}
			String bank=optString("bank",request);
			if (bank!=null){
				groupleaderbank.setBank(bank);
			}
			String bankCode=optString("bankCode",request);
			if (bankCode!=null){
				groupleaderbank.setBankCode(bankCode);
			}
			String branch=optString("branch",request);
			if (branch!=null){
				groupleaderbank.setBranch(branch);
			}
			String branchCode=optString("branchCode",request);
			if (branchCode!=null){
				groupleaderbank.setBranchCode(branchCode);
			}
			String bankCardNo=optString("bankCardNo",request);
			if (bankCardNo!=null){
				groupleaderbank.setBankCardNo(bankCardNo);
			}
			String province=optString("province",request);
			if (province!=null){
				groupleaderbank.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				groupleaderbank.setCity(city);
			}
			String bankFront=optString("bankFront",request);
			if (bankFront!=null&&bankFront.length()>0){
				groupleaderbank.setBankFront(bankFront);
			}
			String bankBehind=optString("bankBehind",request);
			if (bankBehind!=null&&bankBehind.length()>0){
				groupleaderbank.setBankBehind(bankBehind);
			}
			//银行卡验证
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("name", realName);
			params.put("idNum", idCard);
			params.put("cardNo", bankCardNo);
			Bankcardrecord bankcardrecord=bankcardrecordService.getByParam(params);
			if (bankcardrecord==null) {
				String result=BankcardUtil.cloundBankcardCheck(realName, idCard, bankCardNo);
				if (result==null) {
					return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "银行卡验证接口异常");
				}
				BankcardObj bankcardObj=JsonUtil.fromJson(result, BankcardObj.class);					
				BankcardData bankcardData=bankcardObj.getData();
				bankcardrecord=new Bankcardrecord(realName, idCard, bankCardNo, bankcardData);
				bankcardrecordService.saveOrUpdate(bankcardrecord);
				if (!bankcardObj.getCode().equals("200000")) {
					return JsonUtil.toJson(CommonInfoUtil.DATAERROR, bankcardObj.getMessage());
				}
			}
			groupleaderbank.setGmtModify(new Date());
			groupleaderbankService.saveOrUpdate(groupleaderbank);
			if (id==null) {
				Groupleader groupleader=groupleaderService.get(groupleaderbank.getLeaderId());
				if (groupleader!=null) {
					groupleader.setIsInformation(1);
					groupleaderService.saveOrUpdate(groupleader);
				}
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除团长提现信息
		*/
		@ResponseBody
		@RequestMapping("delGroupleaderbankByIds")
		public String delGroupleaderbankByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Groupleaderbank> groupleaderbankList= groupleaderbankService.getByParams(params);
			if (groupleaderbankList==null||groupleaderbankList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Groupleaderbank groupleaderbank : groupleaderbankList) {
				groupleaderbank.setDelStatus(DelStatus.DEL);
			}
			groupleaderbankService.saveOrUpdateAll(groupleaderbankList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		
		/**
		 * M-26
		 * 查询团长提现信息列表
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/getGroupleaderbank")
		public String getGroupleaderbank(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", user.getLeaderId());
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Groupleaderbank> groupleaderbankList=groupleaderbankService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (groupleaderbankList==null||groupleaderbankList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=groupleaderbankService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, groupleaderbankList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * M-27
		 * 查询团长提现信息详情
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/getGroupleaderbankById")
		public String getGroupleaderbankById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Groupleaderbank groupleaderbank=groupleaderbankService.get(id);
			if (groupleaderbank==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",groupleaderbank);//jsonObject	json对象
		}

		/**
		 * 团长提现银行卡验证
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/checkBankcard")
		public String checkBankcard(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String name=getString("name", request);
			String idNum=getString("idNum", request);
			String cardNo=getString("cardNo", request);
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("name", name);
			params.put("idNum", idNum);
			params.put("cardNo", cardNo);
			Bankcardrecord bankcardrecord=bankcardrecordService.getByParam(params);
			if (bankcardrecord==null) {
				String result=BankcardUtil.cloundBankcardCheck(name, idNum, cardNo);
				if (result==null) {
					return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "银行卡验证接口异常");
				}
				BankcardObj bankcardObj=JsonUtil.fromJson(result, BankcardObj.class);
				if (!bankcardObj.getCode().equals("200000")) {
					return JsonUtil.toJson(CommonInfoUtil.DATAERROR, bankcardObj.getMessage());
				}	
				BankcardData bankcardData=bankcardObj.getData();
				bankcardrecord=new Bankcardrecord(name, idNum, cardNo, bankcardData);
				bankcardrecordService.saveOrUpdate(bankcardrecord);
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",bankcardrecord);//jsonObject	json对象
		}
		
		
}
